package server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;

public class Logger {
	private final static String FOLDER = "log/";
	private final static String EXTENSION = ".log";
	private final static Boolean APPEND = true;

	static public void auditAction(String userID, String eventMessage) throws IOException {
		PrintWriter writer = new PrintWriter(new BufferedWriter(
				new FileWriter(FOLDER + getDate() + EXTENSION, APPEND)));
		writer.println(getTime() + "\t\t" + userID + "\t\t" + eventMessage);
		writer.close();
	}	

	static private String getDate() {
		GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
				.getInstance();
		String month = "";
		if ((calendar.get(GregorianCalendar.MONTH)) < 10)
			month = "0";
		String date = "";
		if ((calendar.get(GregorianCalendar.DATE)) < 10)
			date = "0";
		return (calendar.get(GregorianCalendar.YEAR)) + month
				+ (calendar.get(GregorianCalendar.MONTH)) + date
				+ (calendar.get(GregorianCalendar.DATE));
	}

	static private String getTime() {
		GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
				.getInstance();
		int hour = (calendar.get(GregorianCalendar.HOUR));
		String writtenHour = "" + hour;
		if (hour < 10)
			writtenHour = "0" + writtenHour;
		int minute = (calendar.get(GregorianCalendar.MINUTE));
		String writtenMinute = "" + minute;
		if (minute < 10)
			writtenMinute = "0" + writtenMinute;
		int second = (calendar.get(GregorianCalendar.HOUR));
		String writtenSecond = "" + second;
		if (second < 10)
			writtenSecond = "0" + writtenSecond;
		return writtenHour + ":" + writtenMinute + ":" + writtenSecond;
	}
}
